/**
 * 
 */
package com.p2p.base.service.impl;

import java.util.Arrays;
import java.util.Random;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.mail.EmailException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.p2p.base.cache.Cache;
import com.p2p.base.email.EmailSender;
import com.p2p.base.exception.ValidateCodeSendException;
import com.p2p.base.model.ValidateCodeSend;
import com.p2p.base.model.ValidateCodeSend.BizTypeEnum;
import com.p2p.base.model.ValidateCodeSend.SendTypeEnum;
import com.p2p.base.result.ValidateCodeSendResult;
import com.p2p.base.service.ValidateCodeSendService;
import com.p2p.base.util.mail.SendCloudMail;
import com.p2p.base.util.sms.SMSService;

/**
 * @author ruiqi
 *
 */

@Service("validateCodeSendService")
public class ValidateCodeSendServiceImpl implements ValidateCodeSendService{

	private final Logger logger = LoggerFactory.getLogger(ValidateCodeSendServiceImpl.class);
	
	@Resource
	private Cache defaultCache;
	
	String ValidateCodePrefix="ValidateCode_";
	
	@Override
	public ValidateCodeSendResult createAndSendCheckCode(
			ValidateCodeSend validateCodeSend) {
		ValidateCodeSendResult result = new ValidateCodeSendResult();
		
		try {
			//获取校验码
			String checkCode = this.getCheckCode(validateCodeSend);
			logger.debug("生成的校验码：" + checkCode);
			//获取内容模板
			String contentTemplate = this.getContentTemplate(validateCodeSend);
			//替换模板内容
			String content = contentTemplate.replaceFirst("\\$\\{checkCode\\}",
					checkCode);
			String subject=validateCodeSend.getBizType().getSubject();
			//发送邮件或短信校验码
			doSend(validateCodeSend.getSendType(),
					validateCodeSend.getSendIdentity(), content,subject);
			String bizName = getBizName(validateCodeSend);
			//写入缓存
			saveCache(validateCodeSend.getSendIdentity(), bizName, checkCode,
					validateCodeSend.getEffectMinute());
		} catch(ValidateCodeSendException e){
			
			logger.error("ValidateCodeSendService.createAndSendCheckCode  excute exception",e);
			result.setResultCode(e.getCode());
			return result;
		}catch (Exception e) {
			result.setResultCode(ValidateCodeSendResult.ResultCodeEnum.System_exception.name());
			logger.error("ValidateCodeSendService.createAndSendCheckCode  excute exception",e);
			return result;
		}
		result.setSuccess(true);
		return result;
	}

	@Override
	public ValidateCodeSendResult check(ValidateCodeSend validateCodeSend,
			String code) {
		
		ValidateCodeSendResult result = new ValidateCodeSendResult();
		//全局验证码，测试用
		if(StringUtils.equals(code, "yqh888")){
			result.setSuccess(true);
			return result;
		}
		String bizName;
		try {
			bizName = getBizName(validateCodeSend);
		} catch (ValidateCodeSendException e) {
			logger.error("ValidateCodeSendService.check  excute exception",e);
			result.setResultCode(e.getCode());
			return result;
		}
		
		Object o = getCache(validateCodeSend.getSendIdentity(),bizName);
		if(o==null){
			result.setResultCode(ValidateCodeSendResult.ResultCodeEnum.ExpiredOrNoExist.name());
			return result;
		}
		String cacheCode = (String)o;
		if(!StringUtils.equals(cacheCode, code)){
			result.setResultCode(ValidateCodeSendResult.ResultCodeEnum.Code_match_error.name());
			return result;
		}
		result.setSuccess(true);
		removeCache(validateCodeSend.getSendIdentity(),bizName);
		return result;
	}
	
	private String getBizName(ValidateCodeSend validateCodeSend) throws ValidateCodeSendException{
		String bizName;
		BizTypeEnum bizType = validateCodeSend.getBizType();
		if(bizType==BizTypeEnum.Other){
			bizName = validateCodeSend.getOtherBizName();
		}else{
			bizName = bizType.name();
		}
		return bizName;
	}
	
	
	private String getContentTemplate(ValidateCodeSend validateCodeSend) throws ValidateCodeSendException{
		String contentTemplate;
		BizTypeEnum bizType = validateCodeSend.getBizType();
		if(bizType == BizTypeEnum.Other){
			contentTemplate = validateCodeSend.getContentTemplate();
		}else{
			contentTemplate = bizType.getContent();
		}
		return contentTemplate;
	}
	
	private String getCheckCode(ValidateCodeSend validateCodeSend) throws ValidateCodeSendException{
		String code;
		SendTypeEnum sendTypeEnum = validateCodeSend.getSendType();
		
		if(sendTypeEnum==SendTypeEnum.SMS){
			code=createMobileCode();
		}else if(sendTypeEnum==SendTypeEnum.EMAIL){
			code=createEmailCode();
		}else{
			code=createMobileCode();
		}
		return code;
	}
	
	private void saveCache(String identity,String bizTypeName,String checkCode,int effectMinute){
		defaultCache.add(ValidateCodePrefix+bizTypeName+"_"+identity, checkCode,effectMinute*60);
	}
	
	private Object getCache(String identity,String bizTypeName){
		return defaultCache.getValue(ValidateCodePrefix+bizTypeName+"_"+identity);
	}
	private void removeCache(String identity,String bizTypeName){
		defaultCache.delete(ValidateCodePrefix+bizTypeName+"_"+identity);
	}
	private void doSend(SendTypeEnum sendTypeEnum,String identity,String content,String subject){
		if(sendTypeEnum==SendTypeEnum.SMS){
			sendSMS(identity,content);
		}else{
			sendEmail(identity,content,subject);
		}
	}
	private void sendSMS(String mobile,String content){
		SMSService.sendSMS(mobile, content);
	}
	
	private void sendEmail(String email,String content,String subject){
	/*	System.out.println("email:"+content);
		EmailSender emailSender = new EmailSender(true);
		try {
			emailSender.sendEmail(Arrays.asList(email), subject, content);
		} catch (EmailException e) {
			logger.error("邮箱发送失败，email="+email);
			e.printStackTrace();
		}*/
		SendCloudMail.send(email, subject, content);
	}
	
	private String createMobileCode(){
		int number = new Random().nextInt(1000000);
		String str1 = String.format("%06d", number);
		return str1;
	}
	
	private String createEmailCode(){
		int number = new Random().nextInt(1000000);
		String str1 = String.format("%06d", number);
		return str1;
	}

	public static void main(String[] args) {
		String contentTemplate = ValidateCodeSend.BizTypeEnum.MailBinding.getContent();
		//System.out.println(contentTemplate);
		//String contentTemplate = "abcccc#{checkCode}";
		System.out.println(contentTemplate);
		String content = contentTemplate.replaceFirst("#\\{checkCode\\}", "12345");
		System.out.println(content);
	}

}
